<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
error_reporting(E_ALL ^ E_NOTICE);
// 应用公共文件
function pr($data)
{
    echo '<pre>';
    print_r($data);
    echo '</pre>';
}

function _pr($data)
{
    pr($data);
    exit;
}

/* 生成密码 */
function createPassword($password)
{
    $string = $password . config('authkey');
    return md5(md5($string));
}

/**
 * 验证手机号码
 */
function checkMobile($mobile)
{
    return preg_match("/^1[3|4|5|6|7|8|9][0-9]\d{8}$/", $mobile);
}

function getMonthNum($date1, $date2) {
    $data1_time = strtotime($date1);
    $data2_time = strtotime($date2);
    $Y = date('Y', $data2_time) - date('Y', $data1_time);
    $M = date('m', $data2_time) - date('m', $data1_time);
    if ($Y == 0) {
        // 年份相等
        $nums = $M + 1;
    } elseif ($Y == 1) {
        // 一年内,开始与12月之间的月份+1,再加结束年的月份
        $nums = 12 - date('m', $data1_time) + 1 + date('m', $data2_time);
    } else {
        // 超过一年的,需要差的年份-1乘以12
        $nums = ($Y - 1) * 12 + 12 - date('m', $data1_time) + 1 + date('m', $data2_time);
    }
    return $nums;
}


/* 密码强度 */
function checkPassword($str)
{
    $score = 0;
    if (preg_match("/[0-9]+/", $str)) {
        $score++;
    }
    if (preg_match("/[0-9]{3,}/", $str)) {
        $score++;
    }
    if (preg_match("/[a-z]+/", $str)) {
        $score++;
    }
    if (preg_match("/[a-z]{3,}/", $str)) {
        $score++;
    }
    if (preg_match("/[A-Z]+/", $str)) {
        $score++;
    }
    if (preg_match("/[A-Z]{3,}/", $str)) {
        $score++;
    }
    if (preg_match("/[_|\-|+|=|*|!|@|#|$|%|^|&|(|)]+/", $str)) {
        $score += 2;
    }
    if (preg_match("/[_|\-|+|=|*|!|@|#|$|%|^|&|(|)]{3,}/", $str)) {
        $score++;
    }
    if (strlen($str) >= 10) {
        $score++;
    }
    return $score;
}

/* 返回数据组装 */
function returnJson($code, $message = '', $data = [])
{
    return json([
        'code' => $code,
        'message' => $message,
        'data' => $data
    ]);
}

/* 相对路径转全路径 */
function tomedia($path)
{
    if (strstr($path, 'http://') || strstr($path, 'https://')) {
        return $path;
    }
    $domain = config('qiniu_domain');
    return $domain . $path;
}

function getDomain()
{
    return isHttps() ? 'https://' : 'http://' . $_SERVER['SERVER_NAME'];
}

function isHttps()
{
    if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
        return true;
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
        return true;
    } elseif (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {
        return true;
    }
    return false;
}

function defaultPage()
{
    $page = input('post.page');
    if ($page <= 0) {
        $page = 1;
    }
    return $page;
}

//生成订单号
function create_order_sn($uid, $prefix = 'SHOP')
{
    return $prefix . date("YmdHis") . sprintf("%05d", $uid) . rand(10, 99);
}

/*
* 获取http://域名
*/
function get_host()
{
    return $this_url = $_SERVER['HTTP_HOST'];
}


/**
 * 异步调用方法
 * @param $host
 * @param $path
 * @param array $param
 */
function doRequest($host, $path, $param = array())
{
    $query = isset($param) ? http_build_query($param) : '';

    $port = 80;
    $errno = 0;
    $errstr = '';
    $timeout = 10;

    $fp = fsockopen($host, $port, $errno, $errstr, $timeout);

    $out = "POST " . $path . " HTTP/1.1\r\n";
    $out .= "host:" . $host . "\r\n";
    $out .= "content-length:" . strlen($query) . "\r\n";
    $out .= "content-type:application/x-www-form-urlencoded\r\n";
    $out .= "connection:close\r\n\r\n";
    $out .= $query;
    $re = fputs($fp, $out);
    fclose($fp);
}

function time_preg($time = 0)
{
    if (!$time) {
        $time = time() - 1;
    }

    if (time() - $time < 60) {
        return time() - $time . '秒前';
    }

    if ((time() - $time) / 60 < 60) {

        return floor((time() - $time) / 60) . '分钟前';
    }

    if ((time() - $time) / 3600 < 24) {
        return floor((time() - $time) / 3600) . '小时前';
    }

    return floor((time() - $time) / (3600 * 24)) . '天前';

}


function image_gd($image){

}